## Bit-product of integers and Hadamard matrices
from PyM import *

def bit_product(a,b,r=''):
    a = reverse(bin(a)[2:]); b = reverse(bin(b)[2:])
    l = min(len(a),len(b))
    if r=='': r = l
    else: r = min(l,r)
    a = [int(x) for x in a][:r]; b = [int(x) for x in b][:r]
    return  dot(a,b) % 2
#
bdot = bit_product
    
def hadamard(r):
    if not isinstance(r,int): return "Error hadamard: parameter is not an integer"
    if not r>0: return "Error hadamard: {} is not a positive integer".format(r)
    n = 2**r
    return matrix([[(-1)**bit_product(i,j,n) for j in range(n)] for i in range(n)])
    
    
p = bit_product(37,41)

show(p)

H = hadamard(2)
show(matrix(H))

show(hadamard(3))
